package com.example.module_base.Base;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import org.jetbrains.annotations.Nullable;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2017/11/3.
 */

public abstract class BaseFragment<B extends ViewDataBinding> extends Fragment {
    private B binding;
    public Context mContext;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater,setlayoutid(),container,false);
        mContext = getContext();
        init(binding);
        return binding.getRoot();
    }
    public abstract int setlayoutid();
    public abstract void init(B binding);
    public void toast(Object s){
        if (getContext()==null)
            return;
        Toast.makeText(getContext(), String.valueOf(s), Toast.LENGTH_SHORT).show();
    }
    //基类
    public void addToolBar(final LinearLayout linearLayout, Toolbar toolbar){
        addToolBar(linearLayout,toolbar,null);
    }
    public void addToolBar(final LinearLayout linearLayout, Toolbar toolbar,Boolean b){
        addToolBar(linearLayout,toolbar, b,null);
    }

    /**
     *
     * @param linearLayout
     * @param toolbar
     * @param b     是否布局下移一个状态栏高度
     * @param b2    是否有副标题
     */
    public void addToolBar(final LinearLayout linearLayout, Toolbar toolbar,Boolean b,Boolean b2){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        // 取控件aaa当前的布局参数
        if (b2==null)params.height = dip2px(45.0f);        // 当控件的高强制设成150象素
        params.width=Toolbar.LayoutParams.MATCH_PARENT;
        if (b!=null&&b==true){
            params.setMargins(0,getStatusBarHeight(),0,0);
            Log.e(TAG, "addToolBar: "+String.valueOf(getStatusBarHeight()) );
        }
        linearLayout.addView(toolbar,0,params);
    }
    public int dip2px(float dip){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dip,getResources().getDisplayMetrics());
    }
    //获取状态栏高度
    private int getStatusBarHeight(){
        int result = 0;
        int resId = getResources().getIdentifier("status_bar_height","dimen","android");
        if(resId>0){
            result = getResources().getDimensionPixelSize(resId);
        }
        return result;
    }
//调用
/**
 Toolbar toolbar=new MyToolBar(this)
     .init().addTitle("登陆")
     .addLogo()
     .setOverflowIcon()
     .addHome(0, new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    finish();
    }
    });
 this.addToolBar(binding.lineToolbar,toolbar);
 */


}

